package com.wsc.wisdomchina.activitity.wisdomParty;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.adapter.InterflowPingLunAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyInterFlowListBean;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyUserBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyListView;
import com.wsc.wisdomchina.view.MyToast;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class PartyChatInfoDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private WebView webView;
    private String idIntent;
    private final String INTERFLOWINFOLISTFLAG="INTERFLOWINFOLISTFLAG";//详情
    private final String FLOWLISTFLAG="FLOWLISTFLAG";//评论列表
    private int skip=0;
    private final int limit=6;
    private CircleImageView civ_userphoto;
    private TextView tv_username;
    private TextView tv_time;
    private TextView tv_headtitle;
    private TextView tv_title;
    private MyListView lv_pinglun;
    private List<PartyInterFlowListBean> homeNewsList;
    private boolean isfresh=true;
    private InterflowPingLunAdapter quanBuAdapter;
    private PullToRefreshScrollView fragment_first_scrollView;
    private EditText et_message;
    private LinearLayout layout_send;
    private MyToast myToast;
    private PartyInterFlowListBean newsDetailBean;
    private final String INTERFLOWADDFLAG="INTERFLOWADDFLAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.partychatinfodetail_layout);
        initView();
        context = this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, this);
        Intent intent=getIntent();
        idIntent= intent.getStringExtra("id");
        tv_headtitle.setText("详情");
        //帖子详情
        contributorPresenter.getInterflowDetail(idIntent,INTERFLOWINFOLISTFLAG);
        //评论列表
        contributorPresenter.getFlowList(idIntent,skip,limit,FLOWLISTFLAG);
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        webView = (WebView) findViewById(R.id.fragment_zxxq_webView);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        civ_userphoto=(CircleImageView) findViewById(R.id.civ_userphoto);
        tv_username = (TextView) findViewById(R.id.tv_username);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_title=(TextView) findViewById(R.id.tv_title);
        lv_pinglun=(MyListView) findViewById(R.id.lv_pinglun);
        et_message=(EditText) findViewById(R.id.et_message);
        layout_send=(LinearLayout) findViewById(R.id.layout_send);
        layout_send.setOnClickListener(this);
        fragment_first_scrollView = (PullToRefreshScrollView)findViewById(R.id.fragment_first_scrollView);
        fragment_first_scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        fragment_first_scrollView.requestFocus();
        fragment_first_scrollView.setFocusable(true);
        fragment_first_scrollView.setFocusableInTouchMode(true);
        Tools.showScrollRefreshView(fragment_first_scrollView);
        fragment_first_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showScrollRefreshView(fragment_first_scrollView);
                    skip = 0;
                    contributorPresenter.getFlowList(idIntent,skip,limit,FLOWLISTFLAG);
                }
                // 处理上拉刷新的业务
             else {
                    isfresh = false;
                    Tools.showScrollRefreshView(fragment_first_scrollView);
                    skip += limit;
                    contributorPresenter.getFlowList(idIntent,skip,limit,FLOWLISTFLAG);
                }
            }
        });

    }


    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(INTERFLOWINFOLISTFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    newsDetailBean = JsonTools.jsonStringToObject(messageJson, PartyInterFlowListBean.class);

                    Glide.with(context).load(newsDetailBean.getHeadImg())
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .placeholder(R.mipmap.own_pic_logout)
                            .into((CircleImageView) civ_userphoto);

                    tv_username.setText(newsDetailBean.getCreateUserName());

                    String date_time = Tools.timedate(String.valueOf(newsDetailBean.getCreateTime() / 1000));
                    tv_time.setText(date_time);

                    tv_title.setText(newsDetailBean.getTitle());

                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                }
            }else if(flag.equals(FLOWLISTFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<PartyInterFlowListBean> homeNewsItem = JsonTools.jsonStringToList(jsonMessage, PartyInterFlowListBean.class);
                    if (homeNewsItem != null && homeNewsItem.size() > 0) {
                        if (isfresh) {
                            lv_pinglun.setVisibility(View.VISIBLE);
                            homeNewsList = homeNewsItem;
                            quanBuAdapter = new InterflowPingLunAdapter(context, homeNewsList);
                            lv_pinglun.setAdapter(quanBuAdapter);
                        } else {
                            homeNewsList.addAll(homeNewsItem);
                            quanBuAdapter.notifyDataSetChanged();
                            lv_pinglun.setSelection(homeNewsList.size());
                        }

                    }
                }
            }else if(flag.equals(INTERFLOWADDFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    myToast.showShort("发表成功");
                    et_message.setText("");
                    isfresh = true;
                    skip = 0;
                    contributorPresenter.getFlowList(idIntent,skip,limit,FLOWLISTFLAG);
                }else {
                    myToast.showShort("发表失败");
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_first_scrollView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.layout_send:
                String et_content=et_message.getText().toString().trim();
                if(et_content.isEmpty())
                {
                    myToast.showShort("评论不能为空");
                }else {
                    String message = Tools.getSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo, "", this);
                    if (message != null && message != "") {
                        PartyUserBean partyUserBean = JsonTools.jsonStringToObject(message, PartyUserBean.class);
                        contributorPresenter.postAddTie(idIntent,null,et_content,null,partyUserBean.getId(),INTERFLOWADDFLAG);
                    }
                }
                break;
        }
    }

}
